import { defineStore } from 'pinia'
// import LayoutMain from '@/components/head/LayoutMain.vue'

let modulesCom = import.meta.glob('../components/*/*.vue')

let modulesView = import.meta.glob('../views/*/*.vue')

export const usePermissionStore = defineStore('permission', {
    // id: 'permission', // id必填，且需要唯一
    state: () => {
        return {
            routes: [],//全部路由
            addRoutes: [],//后端增加的路由
            getRoutes: [],//获取到的格式化好的lu'you
        }
    },
    actions: {
        async getMenuList(): Promise<any> {
            return new Promise((resolve) => {
                let menuList = reactive([
                    {
                        path: '/',
                        url: "home",
                        component: "head/LayoutMain",
                        menuType: 1,
                        name:"首页",
                        meta: {
                            title: "首页",
                            icon: "HomeFilled",
                            roles: ["sys:manage"]
                        },
                        children: []
                    },
                    {
                        path: "/system",
                        url: "user/index1",
                        component: "dd",
                        menuType: 1,
                        alwaysShow: true,
                        name: "系统管理",
                        meta: {
                            title: "系统管理",
                            icon: "Menu",
                            roles: ["sys:manage"],
                            parentId: 0,
                        },
                        children: [
                            {
                                path: "/medicine",
                                url: "medicines/index",
                                component: "head/LayoutMain",
                                menuType: 1,
                                alwaysShow: false,
                                name: "药品中心",
                                meta: {
                                    title: "药品中心",
                                    icon: "Setting",
                                    // roles: ["sys:medicineManage"],
                                    parentId: 1,
                                },
                            },
                            {
                                path: "/user",
                                url: "user/index",
                                component: "head/LayoutMain",
                                menuType: 1,
                                alwaysShow: false,
                                name: "用户管理",
                                meta: {
                                    title: "用户管理",
                                    icon: "UserFilled",
                                    // roles: ["sys:user"],
                                    parentId: 2,
                                },
                            },
                            {
                                path: "/menuList",
                                url: "user/index2",
                                component: "head/LayoutMain",
                                menuType: 1,
                                alwaysShow: false,
                                name: "权限管理",
                                meta: {
                                    title: "权限管理",
                                    icon: "Tickets",
                                    // roles: ["sys:menu"],
                                    parentId: 3,
                                },
                            },
                        ],
                    },
                    {
                        path: "/goods",
                        url: "mediciness/index3",
                        component: "dd",
                        menuType: 1,
                        alwaysShow: true,
                        name: "商品管理",
                        meta: {
                            title: "商品管理",
                            icon: "Shop",
                            roles: ["sys:goods"],
                            parentId: 0,
                        },
                        children: [
                            {
                                path: "/goodCategory",
                                url: "medicine/index4",
                                component: "head/LayoutMain",
                                menuType: 1,
                                alwaysShow: false,
                                name: "商品分类",
                                meta: {
                                    title: "商品分类",
                                    icon: "Van",
                                    roles: ["sys:goodsCategory"],
                                    parentId: 34,
                                },
                            },
                        ],
                    },
                    {
                        path: "/systenConfig",
                        url: "user/index5",
                        component: "dd",
                        menuType: 1,
                        alwaysShow: true,
                        name: "系统工具",
                        meta: {
                            title: "系统工具",
                            icon: "Setting",
                            roles: ["sys:systenConfig"],
                            parentId: 0,
                        },
                        children: [
                            {
                                path: "/document",
                                url: "user/index6",
                                component: "head/LayoutMain",
                                menuType: 1,
                                alwaysShow: false,
                                name: "http://42.193.158.170:8089/swagger-ui/index.html",
                                meta: {
                                    title: "接口文档",
                                    icon: "Tickets",
                                    roles: ["sys:document"],
                                    parentId: 42,
                                },
                            },
                        ],
                    },
                ])
                resolve(menuList)
            });

        },
        async initMenu(router: any) {
            // let menu:any=[]
            let menu:any = [
            //     {
            //     "path": "/user",
            //     "url": "user/index",
            //     "name": "用户列表",
            //     "icon": "ddd",
            //     "menuType": 1,
            //     "children": [
            //         {
            //             "url": "user/index",
            //             "path": "/user",
            //         }
            //     ],
            //     "component": "head/LayoutMain"
            // },
            //     {
            //     "path": "/medicineinfo",
            //     "url": "userinfo/index",
            //     "name": "用户列表dd",
            //     "icon": "ddd",
            //     "menuType": 1,
            //     "children": [
            //         {
            //             "url": "userinfo/index",
            //             "path": "/medicineinfo",
            //         }
            //     ],
            //     "component": "head/LayoutMain"
            // },
        ];
            await this.getMenuList().then((data) => {
                menu = data
            })
            // 动态路由请求成功后，将路由格式化成vue能识别的路由形式
            var fmtRouters = this.formatRouters(menu);

            // 将格式化好的路由添加到router中
            fmtRouters.forEach((fmtRouter: any) => router.addRoute(fmtRouter));
            
            this.getRoutes = fmtRouters
            localStorage.setItem("isSetRouter","1")
        },
        /**
* 格式化动态路由
*
* @param menus 动态路由
*/
        formatRouters(menus: any) {
            if (menus.legth == 0) {
                return;
            }

            let fmtRouters: any = [];
            menus.forEach((menu: any) => {
                let { name, path, component, children, menuType, url } = menu;

                // 递归格式化children路由
                if (children == undefined) {
                    //children参数直接没有
                    const fmtRouterTemp = {
                        path: path,
                        component: modulesView[('../views/' + url + '.vue')]
                    }
                    let fmtRoutersTemp: any = [];
                    fmtRoutersTemp.push(fmtRouterTemp)
                    children = fmtRoutersTemp
                } else if (children && children instanceof Array) {
                    children = this.formatRouters(children);
                }

                const fmtRouter = {
                    name: name,
                    path: path,
                    meta: {
                        requiresAuth: true,
                        title: name,
                    },
                    // component:  modulesCom[('../components/head/LayoutMain.vue')],
                    component: modulesView[('../views/' + url + '.vue')],
                    // component: modulesView[('..')],
                    children: children
                }
                // fmtRouter.name=name
                if (menuType && menuType == 1) {
                    fmtRouter.component = modulesCom[('../components/' + component + '.vue')]
                }

                fmtRouters.push(fmtRouter);
            });

            return fmtRouters;
        }

    }, persist: true
})
